package com.Xht.www.servlet;

//后端响应管理员封禁用户的请求

import com.Xht.www.dao.UserInfoDAO;
import com.Xht.www.po.UserInfoPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/ManageUserStatusServlet")
public class ManageUserStatusServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        int userId = Integer.parseInt(request.getParameter("userId"));

        UserInfoDAO dao = new UserInfoDAO();
        UserInfoPO user = dao.getUserById(userId);

        if (user == null) {
            out.print("{\"success\":false,\"message\":\"用户不存在\"}");
            return;
        }

        boolean result = false;
        if ("ban".equals(action)) {
            result = dao.updateUserStatusToBanned(user.getUsername());
        } else if ("unban".equals(action)) {
            // 原解封逻辑（设为解封中）
            result = dao.updateUserStatusToUnblocking(user.getUsername());
        } else if ("normal".equals(action)) {
            // 新增：将解封中状态直接设为正常
            result = dao.updateUserStatusToNormal(user.getUsername());
        }

        if (result) {
            out.print("{\"success\":true}");
        } else {
            out.print("{\"success\":false,\"message\":\"操作失败\"}");
        }
    }
}
